package classes;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.util.ArrayList;

import ontology.concepts.employmentlaw.*;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import javax.swing.border.BevelBorder;
import javax.swing.JProgressBar;

public class CaseInfo extends JFrame 
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JPanel jPanel = null;
	private JLabel caseid = null;
	private JLabel similarity = null;
	private JLabel utility = null;
	private ArrayList<Norm> laws = new ArrayList();  //  @jve:decl-index=0:
	private JTree jTree = null;
	private JScrollPane jScrollPane1 = null;
	private JPanel jPanel1 = null;
	private JEditorPane jEditorPane = null;
	private JScrollPane jScrollPane = null;
	private JPanel jPanel2 = null;
	private JProgressBar jProgressBar1 = null;
	private JProgressBar jProgressBar2 = null;
	public ArrayList<Norm> getLaws() {
		return laws;
	}

	public void setLaws(ArrayList<Norm> laws) 
	{
		this.laws = laws;
		System.out.println(laws);
		
	}

	/**
	 * This is the default constructor
	 */
	public CaseInfo(ArrayList laws) 
	{
		this.laws = laws;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() 
	{
		this.setSize(500, 500);
		this.setContentPane(getJContentPane());
		this.setTitle("UMCourt Case Visualization");
	}

	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Similarity:");
			jLabel2.setBounds(new Rectangle(141, 30, 75, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("Utility:");
			jLabel1.setBounds(new Rectangle(141, 48, 75, 16));
			jLabel = new JLabel();
			jLabel.setText("Case ID:");
			jLabel.setBounds(new Rectangle(141, 12, 75, 16));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJScrollPane1(), null);
			//jContentPane.add(jTree, null);
			jContentPane.add(getJPanel1(), null);
			//jContentPane.add(jTree, null);
			jContentPane.add(getJScrollPane(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			utility = new JLabel();
			utility.setText("JLabel");
			utility.setBounds(new Rectangle(220, 48, 117, 16));
			similarity = new JLabel();
			similarity.setText("JLabel");
			similarity.setBounds(new Rectangle(220, 30, 117, 16));
			caseid = new JLabel();
			caseid.setText("JLabel");
			caseid.setBounds(new Rectangle(220, 12, 117, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(15, 15, 451, 77));
			jPanel.add(jLabel, null);
			jPanel.add(caseid, null);
			jPanel.add(jLabel2, null);
			jPanel.add(similarity, null);
			jPanel.add(jLabel1, null);
			jPanel.add(utility, null);
			jPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
			jPanel.add(getJPanel2(), null);
			jPanel.add(getJProgressBar1(), null);
			jPanel.add(getJProgressBar2(), null);
		}
		return jPanel;
	}

	public JLabel getCaseid() {
		return caseid;
	}

	public void setCaseid(String caseid) {
		this.caseid.setText(caseid); ;
	}

	public JLabel getSimilarity() {
		return similarity;
	}

	public void setSimilarity(String similarity) 
	{
		this.similarity.setText(similarity);
		jProgressBar1.setMaximum(100);
		jProgressBar1.setMinimum(0);
		jProgressBar1.setValue((int)((Double)(Double.parseDouble(similarity))*100));
	}

	public JLabel getUtility() {
		return utility;
	}

	public void setUtility(String utility, Double maxUtil) 
	{
		this.utility.setText(utility);
		jProgressBar2.setMaximum(maxUtil.intValue());
		jProgressBar2.setValue(((Double)Double.parseDouble(utility)).intValue());
	}

	/**
	 * This method initializes jTree	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JTree getJTree() 
	{
		DefaultMutableTreeNode top = new DefaultMutableTreeNode("Norms Addressed");
		ArrayList<String> articlesAlreadyAddressed = new ArrayList<String>();
		
		//create nodes
		for (int i=0;i<laws.size();i++)
		{
			String currentArticle = laws.get(i).getArticle();
			String currentNum = laws.get(i).getNumber();
			String currentItem = laws.get(i).getItem();
			String currentDesc = laws.get(i).getDescription();
			
			if (!articlesAlreadyAddressed.contains(currentArticle))
			{
				DefaultMutableTreeNode art = new DefaultMutableTreeNode(currentArticle);
				DefaultMutableTreeNode it  = new DefaultMutableTreeNode(currentItem);
				DefaultMutableTreeNode nu  = new DefaultMutableTreeNode(currentNum);
				DefaultMutableTreeNode desc  = new DefaultMutableTreeNode(currentDesc);
				
				it.add(desc);
				nu.add(it);
				art.add(nu);
				top.add(art);
				
				articlesAlreadyAddressed.add(currentArticle);
				//add this norm to the tree
				//check for all norms that have same article
				for (int j=i+1;j<laws.size();j++)
				{
					Norm temp = laws.get(j);
					if (temp.getArticle().equals(currentArticle))
					{
						DefaultMutableTreeNode tit  = new DefaultMutableTreeNode(temp.getItem());
						DefaultMutableTreeNode tnu  = new DefaultMutableTreeNode(temp.getNumber());
						DefaultMutableTreeNode tdesc  = new DefaultMutableTreeNode(temp.getDescription());
						
						if (temp.getNumber().equals(currentNum))
						{
							if (temp.getItem().equals(currentItem))
							{
								it.add(tdesc);
								nu.add(it);
								art.add(nu);
							}
							else
							{
								tit.add(tdesc);
								nu.add(tit);
								art.add(nu);
							}
						}
						else
						{
							tit.add(tdesc);
							tnu.add(tit);
							art.add(tnu);
						}
					}
				}
			}
				
			
		}
		
	    jTree = new JTree(top);
	    //Set leaf icon
		ImageIcon leafIcon = new ImageIcon("imgs/norm.jpg");
		ImageIcon closeIcon = new ImageIcon("imgs/more.jpg");
		ImageIcon openIcon = new ImageIcon("imgs/less.jpg");
		if (leafIcon != null) 
		{
		    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		    renderer.setLeafIcon(leafIcon);
		    renderer.setClosedIcon(closeIcon);
		    renderer.setOpenIcon(openIcon);
		    renderer.setBackgroundNonSelectionColor(new Color(217,255,179));
		    jTree.setCellRenderer(renderer);
		}
		jTree.setBackground(new Color(217,255,179));
	    jTree.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener()
	    {
	    	public void treeExpanded(javax.swing.event.TreeExpansionEvent e) 
	    	{
	    		System.out.println("treeExpanded()"); // TODO Auto-generated Event stub treeExpanded()
	    	}
	    	public void treeCollapsed(javax.swing.event.TreeExpansionEvent e) 
	    	{
	    	}
	    });
	    jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() 
	    {
	    	public void valueChanged(javax.swing.event.TreeSelectionEvent e) 
	    	{
	    		if (e.getPath().getPathCount() == 5)
	    		{
	    			Object[] path = e.getPath().getPath();
	    			jEditorPane.setText("<HTML><b>Art. "+path[1]+", Num. "+path[2]+", Item "+path[3]+"</b><br><br>"+path[4].toString()+"</HTML>");
	    		}
	    	}
	    });
	    return(jTree);
		/*
		if (jTree == null) {
			jTree = new JTree();
			jTree.setBounds(new Rectangle(15, 105, 166, 136));
		}
		return jTree;
		*/
	}



	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane(getJTree());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBounds(new Rectangle(19, 106, 223, 300));
			jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jPanel1.add(getJScrollPane1(), gridBagConstraints);
			//jPanel1.add(jTree, gridBagConstraints1);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jEditorPane	
	 * 	
	 * @return javax.swing.JEditorPane	
	 */
	private JEditorPane getJEditorPane() {
		if (jEditorPane == null) {
			jEditorPane = new JEditorPane();
			jEditorPane.setContentType("text/html");
		}
		return jEditorPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(269, 106, 198, 136));
			jScrollPane.setViewportView(getJEditorPane());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel()
			{
				public void paintComponent(Graphics g) 
			    {
			        Graphics2D g2d = (Graphics2D) g;
			        Image image = new ImageIcon("imgs\\fileSmall.jpg").getImage();
			        g.drawImage(image, 0, 0, this);
			        this.setVisible(true);
			       //g.drawImage(img, x, y, observer)
			        
			    }
			};
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.setBounds(new Rectangle(10, 9, 60, 59));
			jPanel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jPanel2.setBackground(new Color(238, 238, 238));
		}
		return jPanel2;
	}

	/**
	 * This method initializes jProgressBar1	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJProgressBar1() {
		if (jProgressBar1 == null) {
			jProgressBar1 = new JProgressBar();
			jProgressBar1.setBounds(new Rectangle(345, 30, 91, 14));
		}
		return jProgressBar1;
	}

	/**
	 * This method initializes jProgressBar2	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJProgressBar2() {
		if (jProgressBar2 == null) {
			jProgressBar2 = new JProgressBar();
			jProgressBar2.setBounds(new Rectangle(345, 48, 91, 14));
		}
		return jProgressBar2;
	}
	

}  //  @jve:decl-index=0:visual-constraint="10,10"
